-- Copyright (c) 2015,2016 Lucky Byte, Inc.
--
-- Summary : 备份历史流水
--
-- Author  : 吴小虎 <xiaohu@lucky-byte.com>
-- Since   : 2016.9.14
--
-- History
--  * 2016.9.14 : 创建此文件
--  * 2016.9.28 : 转移今天之前的所有流水记录
--  * 2016.9.28 : 转移助农取款订单记录
--  * 2016.10.16 : 分片转移，避免 JDBC 超时
--
-- today = core.date()
-- date_point = today:adddays(-10)

-- 转移流水记录
-- 新版本，一次转移少量的数据，分多次转移
while true do
    local _t = jdbc.select([[
        select count(*)::int as count from pay_trlist
            where sys_time::date < CURRENT_DATE
    ]])
    local _count = _t.get_record(0).get('count')
    logger.info('剩余[%s]条流水记录等待转移...', _count)
    if _count == 0 then
        logger.debug('流水记录转移完毕.')
        break
    end
    jdbc.update([[
        with result as (
            delete from pay_trlist where serial in (
                select serial from pay_trlist
                    where sys_time::date < CURRENT_DATE
                    order by serial limit 1000
            ) returning *
        ) insert into pay_trlist_hist (
            serial, uuid, uuid_no, sys_time,
            incoming, chnl, indirect, trinfo, merch, status, netinfo,
            i_pkt, o_pkt, n_pkt, flags, fee, dcflag, notes
        ) (
            select serial, uuid, uuid_no, sys_time,
            	incoming, chnl, indirect, trinfo, merch, status, netinfo,
            	i_pkt, o_pkt, n_pkt, flags, fee, dcflag, notes
            from result
        )
    ]])
end
--[[ 旧版本，一次性转移所有数据，take 太长的时间，导致 JDBC 超时
jdbc.update([ [
    with result as (
        delete from pay_trlist where sys_time::date < CURRENT_DATE returning *
    ) insert into pay_trlist_hist (
        serial, uuid, uuid_no, sys_time,
        incoming, chnl, indirect, trinfo, merch, status, netinfo,
        i_pkt, o_pkt, n_pkt, flags, fee, dcflag, notes
    ) (
        select serial, uuid, uuid_no, sys_time,
        	incoming, chnl, indirect, trinfo, merch, status, netinfo,
        	i_pkt, o_pkt, n_pkt, flags, fee, dcflag, notes
        from result
    )
] ])
]]

-- 转移助农取款订单记录
while true do
    local _t = jdbc.select([[
        select count(*)::int as count from pay_p2_order
            where sys_time::date < CURRENT_DATE
    ]])
    local _count = _t.get_record(0).get('count')
    logger.info('剩余[%s]条助农代付订单记录等待转移...', _count)
    if _count == 0 then
        logger.debug('助农代付订单记录转移完毕.')
        break
    end
    jdbc.update([[
        with result as (
            delete from pay_p2_order where serial in (
                select serial from pay_p2_order
                    where sys_time::date < CURRENT_DATE
                    order by serial limit 1000
            ) returning *
        ) insert into pay_p2_order_hist (
            serial, uuid, sys_time, p2_uuid, t60_uuid, t40_uuid,
            p2_fee, fee_take_from, p2_fund_uuid,
            recover, recvr_uuid, t40_uuid_old,
            status, received, notes
        ) (
            select serial, uuid, sys_time, p2_uuid, t60_uuid, t40_uuid,
                p2_fee, fee_take_from, p2_fund_uuid,
                recover, recvr_uuid, t40_uuid_old,
                status, received, notes
            from result
        )
    ]])
end

--[[
jdbc.update([ [
    with result as (
        delete from pay_p2_order where sys_time::date < CURRENT_DATE returning *
    ) insert into pay_p2_order_hist (
        serial, uuid, sys_time, p2_uuid, t60_uuid, t40_uuid,
        p2_fee, fee_take_from, p2_fund_uuid,
        recover, recvr_uuid, t40_uuid_old,
        status, received, notes
    ) (
        select serial, uuid, sys_time, p2_uuid, t60_uuid, t40_uuid,
            p2_fee, fee_take_from, p2_fund_uuid,
            recover, recvr_uuid, t40_uuid_old,
            status, received, notes
        from result
    )
] ])
]]

-- 转移助农取款资金池变动历史
while true do
    local _t = jdbc.select([[
        select count(*)::int as count from pay_p2_fund_alter
            where ctime::date < CURRENT_DATE
    ]])
    local _count = _t.get_record(0).get('count')
    logger.info('剩余[%s]条资金池变更记录等待转移...', _count)
    if _count == 0 then
        logger.debug('资金池变更记录转移完毕.')
        break
    end
    jdbc.update([[
        with result as (
            delete from pay_p2_fund_alter where serial in (
                select serial from pay_p2_fund_alter
                    where ctime::date < CURRENT_DATE
                    order by serial limit 1000
            ) returning *
        ) insert into pay_p2_fund_alter_hist (
            serial, ctime, p2_fund, type, remains, amt, merno, termid, notes
        ) (
            select serial, ctime, p2_fund, type, remains, amt, merno, termid, notes
            from result
        )
    ]])
end

--[[
jdbc.update([ [
    with result as (
        delete from pay_p2_fund_alter where ctime::date < CURRENT_DATE returning *
    ) insert into pay_p2_fund_alter_hist (
        serial, ctime, p2_fund, type, remains, amt, merno, termid, notes
    ) (
        select serial, ctime, p2_fund, type, remains, amt, merno, termid, notes
        from result
    )
] ])
]]
